package com.huika.renrench.control.my.account;

import java.util.HashMap;

import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.validation.EditTextValidator;
import com.huika.lib.validation.ValidationModel;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.Constant;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.login.validations.VerifyNumberValidation;
import com.huika.renrench.control.my.event.BindPhoneEvent;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.UserModel;
import com.huika.renrench.view.TitleBarHelper;
import com.huika.renrench.view.clearedit.ClearableEditText;

import de.greenrobot.event.EventBus;
/**
* @ClassName: ModifyPhoneActivity 
* @说明:验证原手机
* @author zwy
* @date 2015年6月5日 上午11:31:44
 */
public class ModifyPhoneActivity extends BaseAct {
	private TextView tvPhone;
	private ClearableEditText etVerfy;
	private TextView btnVerfy;
	private Button btnNext;

	private EditTextValidator editTextValidator;
	
	private String phone ;
	private String account;
	private String userId;

	@Override
	public void setRootView() {
		setContentView(R.layout.activity_modify_phone);
		EventBus.getDefault().register(this);
	}
	
	public void onEventMainThread(BindPhoneEvent bindPhoneEvent) {
		this.finish();
	}

	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}
	
	@Override
	protected void initData() {
		UserModel userModel =GlobalApp.userModel;
		phone = userModel.getPhone();
		account = userModel.getAccount();
		userId = userModel.getUserId();
	}

	@Override
	protected void initWidget() {
		TitleBarHelper titleBarHelper = new TitleBarHelper(this, -1, -1, R.string.title_modify_phone_verfy);
		titleBarHelper.setLeftMsg(R.string.left_back);
		tvPhone = (TextView) findViewById(R.id.tv_phone_amp);
		etVerfy = (ClearableEditText) findViewById(R.id.et_verfy_amp);
		btnVerfy = (TextView) findViewById(R.id.btn_verfy_amp);
		btnNext = (Button) findViewById(R.id.btn_next_amp);
		setVerfyEt(false);
		btnVerfy.setOnClickListener(this);
		btnNext.setOnClickListener(this);
		tvPhone.setText(String.format("%s****%s", phone.substring(0, 4),phone.substring(8, 11)));
		editTextValidator = new EditTextValidator(this).add(new ValidationModel(etVerfy, new VerifyNumberValidation())).execute();
	}
	
	
	@Override
	public void widgetClick(View v) {
		switch (v.getId()) {
		case R.id.btn_verfy_amp:
			verfyNet();
			break;
		case R.id.btn_next_amp:
			next();
			break;

		default:
			break;
		}
	}

	private void verfyNet() {
		showLoadingDialog("");
		FormResultRequest<String> request = new FormResultRequest<String>(UrlConstants.GETMSGBINDCHANGEPHONEGETCODEREQ, codeListener, this, new TypeToken<RequestResult<String>>() {
		}.getType());
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putStringTypeParam("userId", userId);
		ajaxParams.putStringTypeParam("phone", phone);
		ajaxParams.putStringTypeParam("account", account);
		ajaxParams.putStringTypeParam("ip", GlobalApp.ip);
		ajaxParams.putStringTypeParam("mac", GlobalApp.mac);
		ajaxParams.putStringTypeParam("dev", Constant.DEV);
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}

	private void next() {
		if (editTextValidator.validate()) {
			FormResultRequest<HashMap<String, String>> request = new FormResultRequest<HashMap<String, String>>(UrlConstants.CHANGEPHONECHECK, verfyListener, this, new TypeToken<RequestResult<HashMap<String, String>>>() {
			}.getType());
			JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
			ajaxParams.putStringTypeParam("userId", userId);
			ajaxParams.putStringTypeParam("validateCode", getInputStr(etVerfy));
			ajaxParams.putStringTypeParam("ip", GlobalApp.ip);
			ajaxParams.putStringTypeParam("mac", GlobalApp.mac);
			ajaxParams.putStringTypeParam("dev", Constant.DEV);
			request.setRequestParams(ajaxParams);
			executeRequest(request);
		}
	}
	
	private Listener<RequestResult<String>> codeListener = new Listener<RequestResult<String>>() {
		@Override
		public void onResponse(RequestResult<String> arg0) {
			dismissLoadingDialog();
			showToastMsg(arg0.msg);
			btnVerfy.setClickable(false);
			setVerfyEt(true);
			new CountDownTimer(120000, 1000) {
				public void onTick(long millisUntilFinished) {
					btnVerfy.setTextColor(getResources().getColor(R.color.color_969696));
					btnVerfy.setText(String.format("%s秒", String.valueOf(millisUntilFinished / 1000)));
				}

				public void onFinish() {
					btnVerfy.setTextColor(getResources().getColor(R.color.color_38bdee));
					btnVerfy.setText("获取验证码");
					btnVerfy.setClickable(true);
				}
			}.start();
		}
	};
	private Listener<RequestResult<HashMap<String, String>>> verfyListener = new Listener<RequestResult<HashMap<String, String>>>() {
		@Override
		public void onResponse(RequestResult<HashMap<String, String>> arg0) {
			dismissLoadingDialog();
			if (arg0.flag==1) {
				Intent intent = new Intent(aty, SetNewPhoneActivity.class);
				intent.putExtra("key", arg0.getRs().get("key"));
				showActivity(aty, intent);
			}else{
				showToastMsg(arg0.msg);
			}
		}
	};
	
	private void setVerfyEt(boolean isShow) {
		if (isShow) {
			etVerfy.setEnabled(true);
		} else {
			etVerfy.setEnabled(false);
		}
	}

}
